<?php

/**
 * Erra_Core_Locale - Handles internationalization methods
 *
 * @package
 * @author Alexis Eric
 * @copyright Copyright (c) 2012 - Erra Prod
 * @version $1.0$
 * @access public
 */

class Erra_Core_Locale {

    /** @var string	Forced text direction (ltr or rtl). If not given, it will be guessed from current language */
    public static $text_direction;

    /** @ignore */
    protected static $langs = array();

    public static function getReloadingMessage($lang='en') {
        $messages = array('en' => 'Reloading...', 'fr' => 'Rechargement...', 'es' => 'Recarga...', 'de' => 'Neuladen....', 'nl' => 'Herladen...', 'pt' => 'Recarregamento...');
        return isset($messages[$lang]) ? $messages[$lang] : $messages['en'];
    }

    public static function applicationIsMultiLang() {
        return count(self::getAvailableLangs()) > 1 ? true : false;
    }

    /**
     * Erra_Core_Locale::getBrowserLang() - Returns browser language
     *
     * @return
     */
    public static function getBrowserLang() {
        $locales = array_keys(self::getAvailableLangs());
        if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
            return $locales[0];
        }
        $dec = explode(';', $_SERVER['HTTP_ACCEPT_LANGUAGE'], 2);
        $langs = explode(',', $dec[0]);
        foreach ($langs as $lang) {
            $lang = substr($lang, 0, 2);
            if (in_array($lang, $locales)) {
                return $lang;
            }
        }
        return $locales[0];
    }

    /**
     * Erra_Core_Locale::set() - Sets a language
     *
     * @param mixed $lang - The language code to set (eg: en, fr, de...)
     * @param mixed $setlocale - true to change server locales
     * @return
     */
    public static function set($lang = null, $setlocale = true) {
        $lang = is_null($lang) ? self::getBrowserLang() : $lang;
        self::init();
        self::setup(DOCUMENT_ROOT . '/library/Erra/L10n/translation.' . $lang);
        self::setup(DOCUMENT_ROOT . '/application/L10n/translation.' . $lang);
        $g = $GLOBALS['__l10n'];

        if ($setlocale) {
            self::setlocales($lang);
        }
        $GLOBALS['__l10n'] = $g;
    }

    /**
     * Erra_Core_Locale::setlocales() - Set server locales
     *
     * @param mixed $lang - The language code to set (eg: en, fr, de...)
     * @param mixed $search
     * @return
     */
    public static function setlocales($lang, $search = true) {
        self::setlocale(LC_TIME, $lang, $search);
        self::setlocale(LC_CTYPE, $lang, $search);
    }

    /**
     * Erra_Core_Locale::isOdd() - Return true if the number is odd
     *
     * @param mixed $i
     * @return
     */
    public static function isOdd($i) {
        return abs($i) % 2 == 1;
    }

    public static function getDefaultLang() {
        $list = array_keys(self::getAvailableLangs());
        return $list[0];
    }

    /**
     * Erra_Core_Locale::getAvailableLangs() - Returns list of available langs
     *
     * @param mixed $flip
     * @return
     */
    public static function getAvailableLangs($flip = false) {
        $res = array();
        $iso = self::getISOcodes();
        if (config_get('application.available_langs')) {
            $lang_list = str_replace(' ', '', config_get('application.available_langs'));
            $langs = explode(',', $lang_list);
        } else {
            $langs = array();
        }

        foreach ($langs as $lang) {
            if (!isset($iso[$lang])) {
                continue;
            }
            $res[$lang] = $iso[$lang];
        }
        if ($flip) {
            return array_flip($res);
        }
        return $res;
    }

    /**
     * Erra_Core_Locale::getCodes() - Returns coutrny codes
     *
     * @param mixed $lang
     * @return
     */
    public static function getCodes($lang = null) {
        if (is_null($lang) || !in_array($lang, array('fr', 'en'))) {
            return self::getISOcodes();
        } else {
            $f = 'getCodes' . ucfirst($lang);
            $res = self::$f();
            asort($res);
            return $res;
        }
    }

    /**
     * Erra_Core_Locale::getCodeByName() - Get
     *
     * @param mixed $name
     * @return
     */
    public static function getCodeByName($name) {
        $t[] = array_flip(self::getCodes('en'));
        $t[] = array_flip(self::getCodes('fr'));
        $t[] = array_flip(self::getCodes());

        foreach ($t as $tt) {
            if (isset($tt[$name])) {
                return $tt[$name];
            }
        }
        return false;
    }

    /**
     * Erra_Core_Locale::getNameByCode() - Get country name by code
     *
     * @param mixed $code
     * @param mixed $lang
     * @return
     */
    public static function getNameByCode($code, $lang = null) {
        $tab = self::getCodes($lang);
        if (isset($tab[$code])) {
            return $tab[$code];
        }
        return false;
    }

    /**
     * Erra_Core_Locale::getCodesFr() - Country code list (french)
     *
     * @return
     */
    public static function getCodesFr() {
        return array('aa' => 'Afar',
            'ab' => 'Abkhaze',
            'af' => 'Afrikaans',
            'ak' => 'Akan',
            'sq' => 'Albanais',
            'am' => 'Amharique',
            'ar' => 'Arabe',
            'an' => 'Aragonais',
            'hy' => 'Arménien',
            'as' => 'Assamais',
            'av' => 'Avar',
            'ae' => 'Avestique',
            'ay' => 'Aymara',
            'az' => 'Azéri',
            'ba' => 'Bachkir',
            'bm' => 'Bambara',
            'eu' => 'Basque',
            'be' => 'Biélorusse',
            'bn' => 'Bengali',
            'bh' => 'Bihari',
            'bi' => 'Bichlamar',
            'bs' => 'Bosniaque',
            'br' => 'Breton',
            'bg' => 'Bulgare',
            'my' => 'Birman',
            'ca' => 'Catalan; valencien',
            'ch' => 'Chamorro',
            'ce' => 'Tchétchène',
            'zh' => 'Chinois',
            'cu' => 'Slavon d\'église; vieux slave; slavon liturgique; vieux bulgare',
            'cv' => 'Tchouvache',
            'kw' => 'Cornique',
            'co' => 'Corse',
            'cr' => 'Cree',
            'cs' => 'Tchèque',
            'da' => 'Danois',
            'dv' => 'Maldivien',
            'nl' => 'Néerlandais; flamand',
            'dz' => 'Dzongkha',
            'en' => 'Anglais',
            'eo' => 'Espéranto',
            'et' => 'Estonien',
            'ee' => 'éwé',
            'fo' => 'FéroÃ¯en',
            'fj' => 'Fidjien',
            'fi' => 'Finnois',
            'fr' => 'Français',
            'fy' => 'Frison occidental',
            'ff' => 'Peul',
            'ka' => 'Géorgien',
            'de' => 'Allemand',
            'gd' => 'Gaélique; gaélique écossais',
            'ga' => 'Irlandais',
            'gl' => 'Galicien',
            'gv' => 'Manx; mannois',
            'el' => 'Grec moderne (après 1453)',
            'gn' => 'Guarani',
            'gu' => 'Goudjrati',
            'ht' => 'HaÃ¯tien; créole haÃ¯tien',
            'ha' => 'Haoussa',
            'he' => 'Hébreu',
            'hz' => 'Herero',
            'hi' => 'Hindi',
            'ho' => 'Hiri motu',
            'hr' => 'Croate',
            'hu' => 'Hongrois',
            'ig' => 'Igbo',
            'is' => 'Islandais',
            'io' => 'Ido',
            'ii' => 'Yi de Sichuan',
            'iu' => 'Inuktitut',
            'ie' => 'Interlingue',
            'ia' => 'Interlingua (langue auxiliaire internationale)',
            'id' => 'Indonésien',
            'ik' => 'Inupiaq',
            'it' => 'Italien',
            'jv' => 'Javanais',
            'ja' => 'Japonais',
            'kl' => 'Groenlandais',
            'kn' => 'Kannada',
            'ks' => 'Kashmiri',
            'kr' => 'Kanouri',
            'kk' => 'Kazakh',
            'km' => 'Khmer central',
            'ki' => 'Kikuyu',
            'rw' => 'Rwanda',
            'ky' => 'Kirghiz',
            'kv' => 'Kom',
            'kg' => 'Kongo',
            'ko' => 'Coréen',
            'kj' => 'Kuanyama; kwanyama',
            'ku' => 'Kurde',
            'l1' => 'International',
            'lo' => 'Lao',
            'la' => 'Latin',
            'lv' => 'Letton',
            'li' => 'Limbourgeois',
            'ln' => 'Lingala',
            'lt' => 'Lituanien',
            'lb' => 'Luxembourgeois',
            'lu' => 'Luba-katanga',
            'lg' => 'Ganda',
            'mk' => 'Macédonien',
            'mh' => 'Marshall',
            'ml' => 'Malayalam',
            'mi' => 'Maori',
            'mr' => 'Marathe',
            'ms' => 'Malais',
            'mg' => 'Malgache',
            'mt' => 'Maltais',
            'mo' => 'Moldave',
            'mn' => 'Mongol',
            'na' => 'Nauruan',
            'nv' => 'Navaho',
            'nr' => 'Ndébélé du Sud',
            'nd' => 'Ndébélé du Nord',
            'ng' => 'Ndonga',
            'ne' => 'Népalais',
            'nn' => 'Norvégien nynorsk; nynorsk, norvégien',
            'nb' => 'Norvégien bokmÃ¥l',
            'no' => 'Norvégien',
            'ny' => 'Chichewa; chewa; nyanja',
            'oc' => 'Occitan (après 1500); provenÃ§al',
            'oj' => 'Ojibwa',
            'or' => 'Oriya',
            'om' => 'Galla',
            'os' => 'Ossète',
            'pa' => 'Pendjabi',
            'fa' => 'Persan',
            'pi' => 'Pali',
            'pl' => 'Polonais',
            'pt' => 'Portugais',
            'ps' => 'Pachto',
            'qu' => 'Quechua',
            'rm' => 'Romanche',
            'ro' => 'Roumain',
            'rn' => 'Rundi',
            'ru' => 'Russe',
            'sg' => 'Sango',
            'sa' => 'Sanskrit',
            'si' => 'Singhalais',
            'sk' => 'Slovaque',
            'sl' => 'Slovène',
            'se' => 'Sami du Nord',
            'sm' => 'Samoan',
            'sn' => 'Shona',
            'sd' => 'Sindhi',
            'so' => 'Somali',
            'st' => 'Sotho du Sud',
            'es' => 'Espagnol; castillan',
            'sc' => 'Sarde',
            'sr' => 'Serbe',
            'ss' => 'Swati',
            'su' => 'Soundanais',
            'sw' => 'Swahili',
            'sv' => 'Suédois',
            'ty' => 'Tahitien',
            'ta' => 'Tamoul',
            'tt' => 'Tatar',
            'te' => 'Télougou',
            'tg' => 'Tadjik',
            'tl' => 'Tagalog',
            'th' => 'ThaÃ¯',
            'bo' => 'Tibétain',
            'ti' => 'Tigrigna',
            'to' => 'Tongan (ÃŽles Tonga)',
            'tn' => 'Tswana',
            'ts' => 'Tsonga',
            'tk' => 'Turkmène',
            'tr' => 'Turc',
            'tw' => 'Twi',
            'ug' => 'OuÃ¯gour',
            'uk' => 'Ukrainien',
            'ur' => 'Ourdou',
            'uz' => 'Ouszbek',
            've' => 'Venda',
            'vi' => 'Vietnamien',
            'vo' => 'VolapÃ¼k',
            'cy' => 'Gallois',
            'wa' => 'Wallon',
            'wo' => 'Wolof',
            'xh' => 'Xhosa',
            'yi' => 'Yiddish',
            'yo' => 'Yoruba',
            'za' => 'Zhuang; chuang',
            'zu' => 'Zoulou');
    }

    /**
     * Erra_Core_Locale::getCodesEn() - Country codes list (en)
     *
     * @return
     */
    public static function getCodesEn() {
        return array('aa' => 'Afar',
            'ab' => 'Abkhazian',
            'af' => 'Afrikaans',
            'ak' => 'Akan',
            'sq' => 'Albanian',
            'am' => 'Amharic',
            'ar' => 'Arabic',
            'an' => 'Aragonese',
            'hy' => 'Armenian',
            'as' => 'Assamese',
            'av' => 'Avaric',
            'ae' => 'Avestan',
            'ay' => 'Aymara',
            'az' => 'Azerbaijani',
            'ba' => 'Bashkir',
            'bm' => 'Bambara',
            'eu' => 'Basque',
            'be' => 'Belarusian',
            'bn' => 'Bengali',
            'bh' => 'Bihari',
            'bi' => 'Bislama',
            'bs' => 'Bosnian',
            'br' => 'Breton',
            'bg' => 'Bulgarian',
            'my' => 'Burmese',
            'ca' => 'Catalan; Valencian',
            'ch' => 'Chamorro',
            'ce' => 'Chechen',
            'zh' => 'Chinese',
            'cu' => 'Church Slavic; Old Slavonic; Church Slavonic; Old Bulgarian; Old Church Slavonic',
            'cv' => 'Chuvash',
            'kw' => 'Cornish',
            'co' => 'Corsican',
            'cr' => 'Cree',
            'cs' => 'Czech',
            'da' => 'Danish',
            'dv' => 'Divehi; Dhivehi; Maldivian',
            'nl' => 'Dutch; Flemish',
            'dz' => 'Dzongkha',
            'en' => 'English',
            'eo' => 'Esperanto',
            'et' => 'Estonian',
            'ee' => 'Ewe',
            'fo' => 'Faroese',
            'fj' => 'Fijian',
            'fi' => 'Finnish',
            'fr' => 'French',
            'fy' => 'Western Frisian',
            'ff' => 'Fulah',
            'ka' => 'Georgian',
            'de' => 'German',
            'gd' => 'Gaelic; Scottish Gaelic',
            'ga' => 'Irish',
            'gl' => 'Galician',
            'gv' => 'Manx',
            'el' => 'Greek, Modern (1453-)',
            'gn' => 'Guarani',
            'gu' => 'Gujarati',
            'ht' => 'Haitian; Haitian Creole',
            'ha' => 'Hausa',
            'he' => 'Hebrew',
            'hz' => 'Herero',
            'hi' => 'Hindi',
            'ho' => 'Hiri Motu',
            'hr' => 'Croatian',
            'hu' => 'Hungarian',
            'ig' => 'Igbo',
            'is' => 'Icelandic',
            'io' => 'Ido',
            'ii' => 'Sichuan Yi; Nuosu',
            'iu' => 'Inuktitut',
            'ie' => 'Interlingue; Occidental',
            'ia' => 'Interlingua (International Auxiliary Language Association)',
            'id' => 'Indonesian',
            'ik' => 'Inupiaq',
            'it' => 'Italian',
            'jv' => 'Javanese',
            'ja' => 'Japanese',
            'kl' => 'Kalaallisut; Greenlandic',
            'kn' => 'Kannada',
            'ks' => 'Kashmiri',
            'kr' => 'Kanuri',
            'kk' => 'Kazakh',
            'km' => 'Central Khmer',
            'ki' => 'Kikuyu; Gikuyu',
            'l1' => 'International',
            'rw' => 'Kinyarwanda',
            'ky' => 'Kirghiz; Kyrgyz',
            'kv' => 'Komi',
            'kg' => 'Kongo',
            'ko' => 'Korean',
            'kj' => 'Kuanyama; Kwanyama',
            'ku' => 'Kurdish',
            'lo' => 'Lao',
            'la' => 'Latin',
            'lv' => 'Latvian',
            'li' => 'Limburgan; Limburger; Limburgish',
            'ln' => 'Lingala',
            'lt' => 'Lithuanian',
            'lb' => 'Luxembourgish; Letzeburgesch',
            'lu' => 'Luba-Katanga',
            'lg' => 'Ganda',
            'mk' => 'Macedonian',
            'mh' => 'Marshallese',
            'ml' => 'Malayalam',
            'mi' => 'Maori',
            'mr' => 'Marathi',
            'ms' => 'Malay',
            'mg' => 'Malagasy',
            'mt' => 'Maltese',
            'mo' => 'Moldavian; Moldovan',
            'mn' => 'Mongolian',
            'na' => 'Nauru',
            'nv' => 'Navajo; Navaho',
            'nr' => 'Ndebele, South; South Ndebele',
            'nd' => 'Ndebele, North; North Ndebele',
            'ng' => 'Ndonga',
            'ne' => 'Nepali',
            'nn' => 'Norwegian Nynorsk; Nynorsk, Norwegian',
            'nb' => 'BokmÃ¥l, Norwegian; Norwegian BokmÃ¥l',
            'no' => 'Norwegian',
            'ny' => 'Chichewa; Chewa; Nyanja',
            'oc' => 'Occitan (post 1500); ProvenÃ§al',
            'oj' => 'Ojibwa',
            'or' => 'Oriya',
            'om' => 'Oromo',
            'os' => 'Ossetian; Ossetic',
            'pa' => 'Panjabi; Punjabi',
            'fa' => 'Persian',
            'pi' => 'Pali',
            'pl' => 'Polish',
            'pt' => 'Portuguese',
            'ps' => 'Pushto; Pashto',
            'qu' => 'Quechua',
            'rm' => 'Romansh',
            'ro' => 'Romanian',
            'rn' => 'Rundi',
            'ru' => 'Russian',
            'sg' => 'Sango',
            'sa' => 'Sanskrit',
            'si' => 'Sinhala; Sinhalese',
            'sk' => 'Slovak',
            'sl' => 'Slovenian',
            'se' => 'Northern Sami',
            'sm' => 'Samoan',
            'sn' => 'Shona',
            'sd' => 'Sindhi',
            'so' => 'Somali',
            'st' => 'Sotho, Southern',
            'es' => 'Spanish; Castilian',
            'sc' => 'Sardinian',
            'sr' => 'Serbian',
            'ss' => 'Swati',
            'su' => 'Sundanese',
            'sw' => 'Swahili',
            'sv' => 'Swedish',
            'ty' => 'Tahitian',
            'ta' => 'Tamil',
            'tt' => 'Tatar',
            'te' => 'Telugu',
            'tg' => 'Tajik',
            'tl' => 'Tagalog',
            'th' => 'Thai',
            'bo' => 'Tibetan',
            'ti' => 'Tigrinya',
            'to' => 'Tonga (Tonga Islands)',
            'tn' => 'Tswana',
            'ts' => 'Tsonga',
            'tk' => 'Turkmen',
            'tr' => 'Turkish',
            'tw' => 'Twi',
            'ug' => 'Uighur; Uyghur',
            'uk' => 'Ukrainian',
            'ur' => 'Urdu',
            'uz' => 'Uzbek',
            've' => 'Venda',
            'vi' => 'Vietnamese',
            'vo' => 'VolapÃ¼k',
            'cy' => 'Welsh',
            'wa' => 'Walloon',
            'wo' => 'Wolof',
            'xh' => 'Xhosa',
            'yi' => 'Yiddish',
            'yo' => 'Yoruba',
            'za' => 'Zhuang; Chuang',
            'zu' => 'Zulu',
        );
    }

    public static function setlocale($type, $lang, $search = true) {
        $t = array('aa' =>
            array('DJ' => 'aa_DJ.utf8',
                'ER' => 'aa_ER',
                'ET' => 'aa_ET',
            ),
            'af' =>
            array('ZA' => 'af_ZA.utf8',
            ),
            'am' =>
            array('ET' => 'am_ET',
            ),
            'an' =>
            array('ES' => 'an_ES.utf8',
            ),
            'ar' =>
            array('AE' => 'ar_AE.utf8',
                'BH' => 'ar_BH.utf8',
                'DZ' => 'ar_DZ.utf8',
                'EG' => 'ar_EG.utf8',
                'IN' => 'ar_IN',
                'IQ' => 'ar_IQ.utf8',
                'JO' => 'ar_JO.utf8',
                'KW' => 'ar_KW.utf8',
                'LB' => 'ar_LB.utf8',
                'LY' => 'ar_LY.utf8',
                'MA' => 'ar_MA.utf8',
                'OM' => 'ar_OM.utf8',
                'QA' => 'ar_QA.utf8',
                'SA' => 'ar_SA.utf8',
                'SD' => 'ar_SD.utf8',
                'SY' => 'ar_SY.utf8',
                'TN' => 'ar_TN.utf8',
                'YE' => 'ar_YE.utf8',
            ),
            'az' =>
            array('AZ' => 'az_AZ.utf8',
            ),
            'be' =>
            array('BY' => 'be_BY.utf8',
            ),
            'bg' =>
            array('BG' => 'bg_BG.utf8',
            ),
            'bn' =>
            array('BD' => 'bn_BD',
                'IN' => 'bn_IN'
            ),
            'br' =>
            array('FR' => 'br_FR.utf8',
            ),
            'bs' =>
            array('BA' => 'bs_BA.utf8',
            ),
            'byn' =>
            array('ER' => 'byn_ER',
            ),
            'ca' =>
            array('ES' => 'ca_ES.utf8',
            ),
            'cs' =>
            array('CZ' => 'cs_CZ.utf8',
            ),
            'cy' =>
            array('GB' => 'cy_GB.utf8',
            ),
            'da' =>
            array('DK' => 'da_DK.utf8',
            ),
            'de' =>
            array('DE' => 'de_DE.utf8',
                'AT' => 'de_AT.utf8',
                'BE' => 'de_BE.utf8',
                'CH' => 'de_CH.utf8',
                'LU' => 'de_LU.utf8',
            ),
            'el' =>
            array('GR' => 'el_GR.utf8',
            ),
            'en' =>
            array('US' => 'en_US.utf8',
                'AU' => 'en_AU.utf8',
                'BW' => 'en_BW.utf8',
                'CA' => 'en_CA.utf8',
                'DK' => 'en_DK.utf8',
                'GB' => 'en_GB.utf8',
                'HK' => 'en_HK.utf8',
                'IE' => 'en_IE.utf8',
                'IN' => 'en_IN',
                'NZ' => 'en_NZ.utf8',
                'PH' => 'en_PH.utf8',
                'SG' => 'en_SG.utf8',
                'ZA' => 'en_ZA.utf8',
                'ZW' => 'en_ZW.utf8',
            ),
            'es' =>
            array('ES' => 'es_ES.utf8',
                'AR' => 'es_AR.utf8',
                'BO' => 'es_BO.utf8',
                'CL' => 'es_CL.utf8',
                'CO' => 'es_CO.utf8',
                'CR' => 'es_CR.utf8',
                'DO' => 'es_DO.utf8',
                'EC' => 'es_EC.utf8',
                'GT' => 'es_GT.utf8',
                'HN' => 'es_HN.utf8',
                'MX' => 'es_MX.utf8',
                'NI' => 'es_NI.utf8',
                'PA' => 'es_PA.utf8',
                'PE' => 'es_PE.utf8',
                'PR' => 'es_PR.utf8',
                'PY' => 'es_PY.utf8',
                'SV' => 'es_SV.utf8',
                'US' => 'es_US.utf8',
                'UY' => 'es_UY.utf8',
                'VE' => 'es_VE.utf8',
            ),
            'et' =>
            array('EE' => 'et_EE.utf8',
            ),
            'eu' =>
            array('ES' => 'eu_ES.utf8',
            ),
            'fa' =>
            array('IR' => 'fa_IR',
            ),
            'fi' =>
            array('FI' => 'fi_FI.utf8',
            ),
            'Faroese' =>
            array('FO' => 'fo_FO.utf8',
            ),
            'fr' =>
            array('FR' => 'fr_FR.utf8',
                'BE' => 'fr_BE.utf8',
                'CA' => 'fr_CA.utf8',
                'CH' => 'fr_CH.utf8',
                'LU' => 'fr_LU.utf8',
            ),
            'ga' =>
            array('IE' => 'ga_IE.utf8',
            ),
            'gd' =>
            array('GB' => 'gd_GB.utf8',
            ),
            'gez' =>
            array('ER' => 'gez_ER',
                'ET' => 'gez_ET',
            ),
            'gl' =>
            array('ES' => 'gl_ES.utf8',
            ),
            'gu' =>
            array('IN' => 'gu_IN',
            ),
            'gv' =>
            array('GB' => 'gv_GB.utf8',
            ),
            'iw' =>
            array('IL' => 'iw_IL.utf8',
            ),
            'hi' =>
            array('IN' => 'hi_IN',
            ),
            'hr' =>
            array('HR' => 'hr_HR.utf8',
            ),
            'hu' =>
            array('HU' => 'hu_HU.utf8',
            ),
            'in' =>
            array('ID' => 'id_ID.utf8',
            ),
            'is' =>
            array('IS' => 'is_IS.utf8',
            ),
            'it' =>
            array('IT' => 'it_IT.utf8',
                'CH' => 'it_CH.utf8',
            ),
            'ja' =>
            array('JP' => 'ja_JP.utf8',
            ),
            'ka' =>
            array('GE' => 'ka_GE.utf8',
            ),
            'kk' =>
            array('KZ' => 'kk_KZ.utf8',
            ),
            'kl' =>
            array('GL' => 'kl_GL.utf8',
            ),
            'kn' =>
            array('IN' => 'kn_IN',
            ),
            'ko' =>
            array('KR' => 'ko_KR.utf8',
            ),
            'kw' =>
            array('GB' => 'kw_GB.utf8',
            ),
            'lg' =>
            array('UG' => 'lg_UG.utf8',
            ),
            'lo' =>
            array('LA' => 'lo_LA',
            ),
            'lt' =>
            array('LT' => 'lt_LT.utf8',
            ),
            'lv' =>
            array('LV' => 'lv_LV.utf8',
            ),
            'mi' =>
            array('NZ' => 'mi_NZ.utf8',
            ),
            'mk' =>
            array('MK' => 'mk_MK.utf8',
            ),
            'ml' =>
            array('IN' => 'ml_IN',
            ),
            'mn' =>
            array('MN' => 'mn_MN',
            ),
            'mr' =>
            array('IN' => 'mr_IN',
            ),
            'ms' =>
            array('MY' => 'ms_MY.utf8',
            ),
            'mt' =>
            array('MT' => 'mt_MT.utf8',
            ),
            'no' =>
            array('NO' => 'no_NO.utf8',
            ),
            'ne' =>
            array('NP' => 'ne_NP',
            ),
            'nl' =>
            array('NL' => 'nl_NL.utf8',
                'BE' => 'nl_BE.utf8',
            ),
            'nn' =>
            array('NO' => 'nn_NO.utf8',
            ),
            'oc' =>
            array('FR' => 'oc_FR.utf8',
            ),
            'om' =>
            array('ET' => 'om_ET',
                'KE' => 'om_KE.utf8',
            ),
            'pa' =>
            array('IN' => 'pa_IN',
            ),
            'pl' =>
            array('PL' => 'pl_PL.utf8',
            ),
            'pt' =>
            array('PT' => 'pt_PT.utf8',
                'BR' => 'pt_BR.utf8',
            ),
            'ro' =>
            array('RO' => 'ro_RO.utf8',
            ),
            'ru' =>
            array('RU' => 'ru_RU.utf8',
                'UA' => 'ru_UA.utf8',
            ),
            'se' =>
            array('NO' => 'se_NO',
            ),
            'sid' =>
            array('ET' => 'sid_ET',
            ),
            'sk' =>
            array('SK' => 'sk_SK.utf8',
            ),
            'sl' =>
            array('SI' => 'sl_SI.utf8',
            ),
            'so' =>
            array('DJ' => 'so_DJ.utf8',
                'ET' => 'so_ET',
                'KE' => 'so_KE.utf8',
                'SO' => 'so_SO.utf8',
            ),
            'sq' =>
            array('AL' => 'sq_AL.utf8',
            ),
            'sr' =>
            array('CS' => 'sr_CS.utf8',
            ),
            'st' =>
            array('ZA' => 'st_ZA.utf8',
            ),
            'sv' =>
            array('SE' => 'sv_SE.utf8',
                'FI' => 'sv_FI.utf8',
            ),
            'ta' =>
            array('IN' => 'ta_IN',
            ),
            'te' =>
            array('IN' => 'te_IN',
            ),
            'tg' =>
            array('TJ' => 'tg_TJ.utf8',
            ),
            'th' =>
            array('TH' => 'th_TH.utf8',
            ),
            'ti' =>
            array('ER' => 'ti_ER',
                'ET' => 'ti_ET',
            ),
            'tig' =>
            array('ER' => 'tig_ER',
            ),
            'tl' =>
            array('PH' => 'tl_PH.utf8',
            ),
            'tr' =>
            array('TR' => 'tr_TR.utf8',
            ),
            'tt' =>
            array('RU' => 'tt_RU.utf8',
            ),
            'uk' =>
            array('UA' => 'uk_UA.utf8',
            ),
            'ur' =>
            array('PK' => 'ur_PK',
            ),
            'uz' =>
            array('UZ' => 'uz_UZ',
            ),
            'vi' =>
            array('VN' => 'vi_VN',
            ),
            'wa' =>
            array('BE' => 'wa_BE.utf8',
            ),
            'xh' =>
            array('ZA' => 'xh_ZA.utf8',
            ),
            'ji' =>
            array('US' => 'yi_US.utf8',
            ),
            'zh' =>
            array('CN' => 'zh_CN.utf8',
                'HK' => 'zh_HK.utf8',
                'SG' => 'zh_SG.utf8',
                'TW' => 'zh_TW.utf8',
            ),
            'zu' =>
            array('ZA' => 'zu_ZA.utf8',
            ),
        );

        if (!$search) {
            $locale = $lang;
        } elseif (strlen($lang) == 2 && isset($t[$lang])) {
            $locale = array_shift($t[$lang]);
        } elseif (strlen($lang) > 2 && stristr($lang, '_')) {
            list($l, $c) = explode('_', $lang);
            if (stristr($c, '.')) {
                list($c, $vide) = explode('.', $c);
            }
            $locale = $t[$l][$c];
        } else {
            $locale = 'en_US';
        }
        if (isset($_SERVER['WINDIR'])) {
            list($l, $utf8) = explode('.', $locale);
            $locale = $l;
        }

        setlocale($type, $locale);
    }

    public static function listEn($std_format=false) {
        $list = array("ALBANIA" => "AL",
            "ALGERIA" => "DZ",
            "AMERICAN SAMOA" => "AS",
            "ANDORRA" => "AD",
            "ANGOLA" => "AO",
            "ANGUILLA" => "AI",
            "ANTARCTICA" => "AQ",
            "ANTIGUA AND BARBUDA" => "AG",
            "ARGENTINA" => "AR",
            "ARMENIA" => "AM",
            "ARUBA" => "AW",
            "AUSTRALIA" => "AU",
            "AUSTRIA" => "AT",
            "AZERBAIJAN" => "AZ",
            "BAHAMAS" => "BS",
            "BAHRAIN" => "BH",
            "BANGLADESH" => "BD",
            "BARBADOS" => "BB",
            "BELARUS" => "BY",
            "BELGIUM" => "BE",
            "BELIZE" => "BZ",
            "BENIN" => "BJ",
            "BERMUDA" => "BM",
            "BHUTAN" => "BT",
            "BOLIVIA" => "BO",
            "BOSNIA AND HERZEGOVINA" => "BA",
            "BOTSWANA" => "BW",
            "BOUVET ISLAND" => "BV",
            "BRAZIL" => "BR",
            "BRITISH INDIAN OCEAN TERRITORY" => "IO",
            "BRUNEI DARUSSALAM" => "BN",
            "BULGARIA" => "BG",
            "BURKINA FASO" => "BF",
            "BURUNDI" => "BI",
            "CAMBODIA" => "KH",
            "CAMEROON" => "CM",
            "CANADA" => "CA",
            "CAPE VERDE" => "CV",
            "CAYMAN ISLANDS" => "KY",
            "CENTRAL AFRICAN REPUBLIC" => "CF",
            "CHAD" => "TD",
            "CHILE" => "CL",
            "CHINA" => "CN",
            "CHRISTMAS ISLAND" => "CX",
            "COCOS (KEELING) ISLANDS" => "CC",
            "COLOMBIA" => "CO",
            "COMOROS" => "KM",
            "CONGO" => "CG",
            "CONGO" => "CD",
            "COOK ISLANDS" => "CK",
            "COSTA RICA" => "CR",
            "CÃ�?TE D'IVOIRE" => "CI",
            "CROATIA" => "HR",
            "CUBA" => "CU",
            "CYPRUS" => "CY",
            "CZECH REPUBLIC" => "CZ",
            "DENMARK" => "DK",
            "DJIBOUTI" => "DJ",
            "DOMINICA" => "DM",
            "DOMINICAN REPUBLIC" => "DO",
            "ECUADOR" => "EC",
            "EGYPT" => "EG",
            "EL SALVADOR" => "SV",
            "EQUATORIAL GUINEA" => "GQ",
            "ERITREA" => "ER",
            "ESTONIA" => "EE",
            "ETHIOPIA" => "ET",
            "FALKLAND ISLANDS" => "FK",
            "FAROE ISLANDS" => "FO",
            "FIJI" => "FJ",
            "FINLAND" => "FI",
            "FRANCE" => "FR",
            "FRENCH GUIANA" => "GF",
            "FRENCH POLYNESIA" => "PF",
            "FRENCH SOUTHERN TERRITORIES" => "TF",
            "GABON" => "GA",
            "GAMBIA" => "GM",
            "GEORGIA" => "GE",
            "GERMANY" => "DE",
            "GHANA" => "GH",
            "GIBRALTAR" => "GI",
            "GREECE" => "GR",
            "GREENLAND" => "GL",
            "GRENADA" => "GD",
            "GUADELOUPE" => "GP",
            "GUAM" => "GU",
            "GUATEMALA" => "GT",
            "GUERNSEY" => "GG",
            "GUINEA" => "GN",
            "GUINEA-BISSAU" => "GW",
            "GUYANA" => "GY",
            "HAITI" => "HT",
            "HEARD ISLAND" => "HM",
            "VATICAN" => "VA",
            "HONDURAS" => "HN",
            "HONG KONG" => "HK",
            "HUNGARY" => "HU",
            "ICELAND" => "IS",
            "INDIA" => "IN",
            "INDONESIA" => "ID",
            "IRAN" => "IR",
            "IRAQ" => "IQ",
            "IRELAND" => "IE",
            "ISLE OF MAN " => "IM",
            "ISRAEL" => "IL",
            "ITALY" => "IT",
            "JAMAICA" => "JM",
            "JAPAN" => "JP",
            "JERSEY " => "JE",
            "JORDAN" => "JO",
            "KAZAKHSTAN" => "KZ",
            "KENYA" => "KE",
            "KIRIBATI" => "KI",
            "NORTH KOREA" => "KP",
            "SOUTH KOREA" => "KR",
            "KUWAIT" => "KW",
            "KYRGYZSTAN" => "KG",
            "LAO PEOPLE'S DEMOCRATIC REPUBLIC" => "LA",
            "LATVIA" => "LV",
            "LEBANON" => "LB",
            "LESOTHO" => "LS",
            "LIBERIA" => "LR",
            "LIBYAN ARAB JAMAHIRIYA" => "LY",
            "LIECHTENSTEIN" => "LI",
            "LITHUANIA" => "LT",
            "LUXEMBOURG" => "LU",
            "MACAO" => "MO",
            "MACEDONIA" => "MK",
            "MADAGASCAR" => "MG",
            "MALAWI" => "MW",
            "MALAYSIA" => "MY",
            "MALDIVES" => "MV",
            "MALI" => "ML",
            "MALTA" => "MT",
            "MARSHALL ISLANDS" => "MH",
            "MARTINIQUE" => "MQ",
            "MAURITANIA" => "MR",
            "MAURITIUS" => "MU",
            "MAYOTTE" => "YT",
            "MEXICO" => "MX",
            "MICRONESIA" => "FM",
            "MOLDOVA, REPUBLIC OF" => "MD",
            "MONACO" => "MC",
            "MONGOLIA" => "MN",
            "MONTSERRAT" => "MS",
            "MOROCCO" => "MA",
            "MOZAMBIQUE" => "MZ",
            "MYANMAR" => "MM",
            "NAMIBIA" => "NA",
            "NAURU" => "NR",
            "NEPAL" => "NP",
            "NETHERLANDS" => "NL",
            "NETHERLANDS ANTILLES" => "AN",
            "NEW CALEDONIA" => "NC",
            "NEW ZEALAND" => "NZ",
            "NICARAGUA" => "NI",
            "NIGER" => "NE",
            "NIGERIA" => "NG",
            "NIUE" => "NU",
            "NORFOLK ISLAND" => "NF",
            "NORTHERN MARIANA ISLANDS" => "MP",
            "NORWAY" => "NO",
            "OMAN" => "OM",
            "PAKISTAN" => "PK",
            "PALAU" => "PW",
            "PALESTINIAN TERRITORY" => "PS",
            "PANAMA" => "PA",
            "PAPUA NEW GUINEA" => "PG",
            "PARAGUAY" => "PY",
            "PERU" => "PE",
            "PHILIPPINES" => "PH",
            "PITCAIRN" => "PN",
            "POLAND" => "PL",
            "PORTUGAL" => "PT",
            "PUERTO RICO" => "PR",
            "QATAR" => "QA",
            "RÃ‰UNION" => "RE",
            "ROMANIA" => "RO",
            "RUSSIAN FEDERATION" => "RU",
            "RWANDA" => "RW",
            "SAINT HELENA" => "SH",
            "SAINT KITTS AND NEVIS" => "KN",
            "SAINT LUCIA" => "LC",
            "SAINT PIERRE AND MIQUELON" => "PM",
            "SAINT VINCENT AND THE GREN." => "VC",
            "SAMOA" => "WS",
            "SAN MARINO" => "SM",
            "SAO TOME AND PRINCIPE" => "ST",
            "SAUDI ARABIA" => "SA",
            "SENEGAL" => "SN",
            "SERBIA AND MONTENEGRO" => "CS",
            "SEYCHELLES" => "SC",
            "SIERRA LEONE" => "SL",
            "SINGAPORE" => "SG",
            "SLOVAKIA" => "SK",
            "SLOVENIA" => "SI",
            "SOLOMON ISLANDS" => "SB",
            "SOMALIA" => "SO",
            "SOUTH AFRICA" => "ZA",
            "SOUTH GEORGIA " => "GS",
            "SPAIN" => "ES",
            "SRI LANKA" => "LK",
            "SUDAN" => "SD",
            "SURINAME" => "SR",
            "SVALBARD AND JAN MAYEN" => "SJ",
            "SWAZILAND" => "SZ",
            "SWEDEN" => "SE",
            "SWITZERLAND" => "CH",
            "SYRIAN ARAB REPUBLIC" => "SY",
            "TAIWAN, PROVINCE OF CHINA" => "TW",
            "TAJIKISTAN" => "TJ",
            "TANZANIA" => "TZ",
            "THAILAND" => "TH",
            "TIMOR-LESTE" => "TL",
            "TOGO" => "TG",
            "TOKELAU" => "TK",
            "TONGA" => "TO",
            "TRINIDAD AND TOBAGO" => "TT",
            "TUNISIA" => "TN",
            "TURKEY" => "TR",
            "TURKMENISTAN" => "TM",
            "TURKS AND CAICOS ISLANDS" => "TC",
            "TUVALU" => "TV",
            "UGANDA" => "UG",
            "UKRAINE" => "UA",
            "UNITED ARAB EMIRATES" => "AE",
            "UNITED KINGDOM" => "GB",
            "UNITED STATES" => "US",
            "UNITED STATES MINOR" => "UM",
            "URUGUAY" => "UY",
            "UZBEKISTAN" => "UZ",
            "VANUATU" => "VU",
            "VENEZUELA" => "VE",
            "VIET NAM" => "VN",
            "VIRGIN ISLANDS, BRITISH" => "VG",
            "VIRGIN ISLANDS, U.S." => "VI",
            "WALLIS AND FUTUNA" => "WF",
            "WESTERN SAHARA" => "EH",
            "YEMEN" => "YE",
            "ZAMBIA" => "ZM",
            "ZIMBABWE" => "ZW");
        if ($std_format == true) {
            $list = array_flip($list);
            $result = array();
            foreach ($list as $code => $country) {
                $result[strtolower($code)] = ucfirst(strtolower($country));
            }
            return $result;
        }
        return $list;
    }

    public static function listFr() {
        return array("INTERNATIONAL" => "INT",
            "UNION EUROPEENNE" => "EU",
            "AFGHANISTAN" => "AF",
            "AFRIQUE DU SUD" => "ZA",
            "Ã…LAND, ÃŽLES" => "AX",
            "ALBANIE" => "AL",
            "ALGÃ‰RIE" => "DZ",
            "ALLEMAGNE" => "DE",
            "ANDORRE" => "AD",
            "ANGOLA" => "AO",
            "ANGUILLA" => "AI",
            "ANTARCTIQUE" => "AQ",
            "ANTIGUA-ET-BARBUDA" => "AG",
            "ANTILLES NÃ‰ERLANDAISES" => "AN",
            "ARABIE SAOUDITE" => "SA",
            "ARGENTINE" => "AR",
            "ARMÃ‰NIE" => "AM",
            "ARUBA" => "AW",
            "AUSTRALIE" => "AU",
            "AUTRICHE" => "AT",
            "AZERBAÃ�?DJAN" => "AZ",
            "BAHAMAS" => "BS",
            "BAHREÃ�?N" => "BH",
            "BANGLADESH" => "BD",
            "BARBADE" => "BB",
            "BÃ‰LARUS" => "BY",
            "BELGIQUE" => "BE",
            "BELIZE" => "BZ",
            "BÃ‰NIN" => "BJ",
            "BERMUDES" => "BM",
            "BHOUTAN" => "BT",
            "BOLIVIE" => "BO",
            "BOSNIE-HERZÃ‰GOVINE" => "BA",
            "BOTSWANA" => "BW",
            "BOUVET, ÃŽLE" => "BV",
            "BRÃ‰SIL" => "BR",
            "BRUNÃ‰I DARUSSALAM" => "BN",
            "BULGARIE" => "BG",
            "BURKINA FASO" => "BF",
            "BURUNDI" => "BI",
            "CAÃ�?MANES, ÃŽLES" => "KY",
            "CAMBODGE" => "KH",
            "CAMEROUN" => "CM",
            "CANADA" => "CA",
            "CAP-VERT" => "CV",
            "CENTRAFRICAINE, RÃ‰PUBLIQUE" => "CF",
            "CHILI" => "CL",
            "CHINE" => "CN",
            "CHRISTMAS, ÃŽLE" => "CX",
            "CHYPRE" => "CY",
            "COCOS (KEELING), ÃŽLES" => "CC",
            "COLOMBIE" => "CO",
            "COMORES" => "KM",
            "CONGO" => "CG",
            "CONGO, LA RÃ‰PUBLIQUE DÃ‰MOCRATIQUE DU" => "CD",
            "COOK, ÃŽLES" => "CK",
            "CORÃ‰E, RÃ‰PUBLIQUE DE" => "KR",
            "CORÃ‰E, RÃ‰PUBLIQUE POPULAIRE DÃ‰MOCRATIQUE DE" => "KP",
            "COSTA RICA" => "CR",
            "CÃ�?TE D'IVOIRE" => "CI",
            "CROATIE" => "HR",
            "CUBA" => "CU",
            "DANEMARK" => "DK",
            "DJIBOUTI" => "DJ",
            "DOMINICAINE, RÃ‰PUBLIQUE" => "DO",
            "DOMINIQUE" => "DM",
            "Ã‰GYPTE" => "EG",
            "EL SALVADOR" => "SV",
            "Ã‰MIRATS ARABES UNIS" => "AE",
            "Ã‰QUATEUR" => "EC",
            "Ã‰RYTHRÃ‰E" => "ER",
            "ESPAGNE" => "ES",
            "ESTONIE" => "EE",
            "Ã‰TATS-UNIS" => "US",
            "Ã‰THIOPIE" => "ET",
            "FALKLAND, ÃŽLES (MALVINAS)" => "FK",
            "FÃ‰ROÃ‰, ÃŽLES" => "FO",
            "FIDJI" => "FJ",
            "FINLANDE" => "FI",
            "FRANCE" => "FR",
            "GABON" => "GA",
            "GAMBIE" => "GM",
            "GÃ‰ORGIE" => "GE",
            "GÃ‰ORGIE DU SUD ET LES ÃŽLES SANDWICH DU SUD" => "GS",
            "GHANA" => "GH",
            "GIBRALTAR" => "GI",
            "GRÃˆCE" => "GR",
            "GRENADE" => "GD",
            "GROENLAND" => "GL",
            "GUADELOUPE" => "GP",
            "GUAM" => "GU",
            "GUATEMALA" => "GT",
            "GUERNESEY" => "GG",
            "GUINÃ‰E" => "GN",
            "GUINÃ‰E-BISSAU" => "GW",
            "GUINÃ‰E Ã‰QUATORIALE" => "GQ",
            "GUYANA" => "GY",
            "GUYANE FRANÃ‡AISE" => "GF",
            "HAÃ�?TI" => "HT",
            "HEARD, ÃŽLE ET MCDONALD, ÃŽLES" => "HM",
            "HONDURAS" => "HN",
            "HONG-KONG" => "HK",
            "HONGRIE" => "HU",
            "ÃŽLE DE MAN" => "IM",
            "ÃŽLES MINEURES Ã‰LOIGNÃ‰ES DES Ã‰TATS-UNIS" => "UM",
            "ÃŽLES VIERGES BRITANNIQUES" => "VG",
            "ÃŽLES VIERGES DES Ã‰TATS-UNIS" => "VI",
            "INDE" => "IN",
            "INDONÃ‰SIE" => "ID",
            "IRAN, RÃ‰PUBLIQUE ISLAMIQUE D'" => "IR",
            "IRAQ" => "IQ",
            "IRLANDE" => "IE",
            "ISLANDE" => "IS",
            "ISRAÃ‹L" => "IL",
            "ITALIE" => "IT",
            "JAMAÃ�?QUE" => "JM",
            "JAPON" => "JP",
            "JERSEY" => "JE",
            "JORDANIE" => "JO",
            "KAZAKHSTAN" => "KZ",
            "KENYA" => "KE",
            "KIRGHIZISTAN" => "KG",
            "KIRIBATI" => "KI",
            "KOWEÃ�?T" => "KW",
            "LAO, RÃ‰PUBLIQUE DÃ‰MOCRATIQUE POPULAIRE" => "LA",
            "LESOTHO" => "LS",
            "LETTONIE" => "LV",
            "LIBAN" => "LB",
            "LIBÃ‰RIA" => "LR",
            "LIBYENNE, JAMAHIRIYA ARABE" => "LY",
            "LIECHTENSTEIN" => "LI",
            "LITUANIE" => "LT",
            "LUXEMBOURG" => "LU",
            "MACAO" => "MO",
            "MACÃ‰DOINE, L'EX-RÃ‰PUBLIQUE YOUGOSLAVE DE" => "MK",
            "MADAGASCAR" => "MG",
            "MALAISIE" => "MY",
            "MALAWI" => "MW",
            "MALDIVES" => "MV",
            "MALI" => "ML",
            "MALTE" => "MT",
            "MARIANNES DU NORD, ÃŽLES" => "MP",
            "MAROC" => "MA",
            "MARSHALL, ÃŽLES" => "MH",
            "MARTINIQUE" => "MQ",
            "MAURICE" => "MU",
            "MAURITANIE" => "MR",
            "MAYOTTE" => "YT",
            "MEXIQUE" => "MX",
            "MICRONÃ‰SIE, Ã‰TATS FÃ‰DÃ‰RÃ‰S DE" => "FM",
            "MOLDOVA, RÃ‰PUBLIQUE DE" => "MD",
            "MONACO" => "MC",
            "MONGOLIE" => "MN",
            "MONTÃ‰NÃ‰GRO" => "ME",
            "MONTSERRAT" => "MS",
            "MOZAMBIQUE" => "MZ",
            "MYANMAR" => "MM",
            "NAMIBIE" => "NA",
            "NAURU" => "NR",
            "NÃ‰PAL" => "NP",
            "NICARAGUA" => "NI",
            "NIGER" => "NE",
            "NIGÃ‰RIA" => "NG",
            "NIUÃ‰" => "NU",
            "NORFOLK, ÃŽLE" => "NF",
            "NORVÃˆGE" => "NO",
            "NOUVELLE-CALÃ‰DONIE" => "NC",
            "NOUVELLE-ZÃ‰LANDE" => "NZ",
            "OCÃ‰AN INDIEN, TERRITOIRE BRITANNIQUE DE L'" => "IO",
            "OMAN" => "OM",
            "OUGANDA" => "UG",
            "OUZBÃ‰KISTAN" => "UZ",
            "PAKISTAN" => "PK",
            "PALAOS" => "PW",
            "PALESTINIEN OCCUPÃ‰, TERRITOIRE" => "PS",
            "PANAMA" => "PA",
            "PAPOUASIE-NOUVELLE-GUINÃ‰E" => "PG",
            "PARAGUAY" => "PY",
            "PAYS-BAS" => "NL",
            "PÃ‰ROU" => "PE",
            "PHILIPPINES" => "PH",
            "PITCAIRN" => "PN",
            "POLOGNE" => "PL",
            "POLYNÃ‰SIE FRANÃ‡AISE" => "PF",
            "PORTO RICO" => "PR",
            "PORTUGAL" => "PT",
            "QATAR" => "QA",
            "RÃ‰UNION" => "RE",
            "ROUMANIE" => "RO",
            "ROYAUME-UNI" => "GB",
            "RUSSIE, FÃ‰DÃ‰RATION DE" => "RU",
            "RWANDA" => "RW",
            "SAHARA OCCIDENTAL" => "EH",
            "SAINT-BARTHÃ‰LEMY" => "BL",
            "SAINTE-HÃ‰LÃˆNE" => "SH",
            "SAINTE-LUCIE" => "LC",
            "SAINT-KITTS-ET-NEVIS" => "KN",
            "SAINT-MARIN" => "SM",
            "SAINT-MARTIN" => "MF",
            "SAINT-PIERRE-ET-MIQUELON" => "PM",
            "SAINT-SIÃˆGE (Ã‰TAT DE LA CITÃ‰ DU VATICAN)" => "VA",
            "SAINT-VINCENT-ET-LES GRENADINES" => "VC",
            "SALOMON, ÃŽLES" => "SB",
            "SAMOA" => "WS",
            "SAMOA AMÃ‰RICAINES" => "AS",
            "SAO TOMÃ‰-ET-PRINCIPE" => "ST",
            "SÃ‰NÃ‰GAL" => "SN",
            "SERBIE" => "RS",
            "SEYCHELLES" => "SC",
            "SIERRA LEONE" => "SL",
            "SINGAPOUR" => "SG",
            "SLOVAQUIE" => "SK",
            "SLOVÃ‰NIE" => "SI",
            "SOMALIE" => "SO",
            "SOUDAN" => "SD",
            "SRI LANKA" => "LK",
            "SUÃˆDE" => "SE",
            "SUISSE" => "CH",
            "SURINAME" => "SR",
            "SVALBARD ET ÃŽLE JAN MAYEN" => "SJ",
            "SWAZILAND" => "SZ",
            "SYRIENNE, RÃ‰PUBLIQUE ARABE" => "SY",
            "TADJIKISTAN" => "TJ",
            "TAÃ�?WAN, PROVINCE DE CHINE" => "TW",
            "TANZANIE, RÃ‰PUBLIQUE-UNIE DE" => "TZ",
            "TCHAD" => "TD",
            "TCHÃˆQUE, RÃ‰PUBLIQUE" => "CZ",
            "TERRES AUSTRALES FRANÃ‡AISES" => "TF",
            "THAÃ�?LANDE" => "TH",
            "TIMOR-LESTE" => "TL",
            "TOGO" => "TG",
            "TOKELAU" => "TK",
            "TONGA" => "TO",
            "TRINITÃ‰-ET-TOBAGO" => "TT",
            "TUNISIE" => "TN",
            "TURKMÃ‰NISTAN" => "TM",
            "TURKS ET CAÃ�?QUES, ÃŽLES" => "TC",
            "TURQUIE" => "TR",
            "TUVALU" => "TV",
            "UKRAINE" => "UA",
            "URUGUAY" => "UY",
            "VANUATU" => "VU",
            "VENEZUELA" => "VE",
            "VIET NAM" => "VN",
            "WALLIS ET FUTUNA" => "WF",
            "YÃ‰MEN" => "YE",
            "ZAMBIE" => "ZM",
            "ZIMBABWE" => "ZW");
    }

    public static function getList($flip = false, $substr = false, $lang = 'fr') {
        if ($lang != 'fr' && $lang != 'en') {
            $lang = 'en';
        }
        $f = 'list' . $lang;
        $t = self::$f();

        if ($flip) {
            return array_flip($t);
        } else {
            return $t;
        }
    }

    /**
     * Erra_Core_Locale::getCode() - Returns code from country name
     *
     * @param mixed $country
     * @param string $lang
     * @return
     */
    public static function getCode($country, $lang = 'en') {
        $t = self::getList(false, false, $lang);
        $country = mb_strtoupper($country);
        return (isset($t[$country])) ? $t[$country] : false;
    }

    /**
     * Erra_Core_Locale::getCountry() - Returns country name from code
     *
     * @param mixed $code
     * @param string $lang
     * @return
     */
    public static function getCountry($code, $lang = 'en') {
        $t = self::getList(true, false, $lang);
        $code = mb_strtoupper($code);
        return (isset($t[$code])) ? $t[$code] : false;
    }

    /**
     * Erra_Core_Locale::getFlag() - Returns flag image based on country code or country name
     *
     * @param mixed $country
     * @param mixed $width
     * @param mixed $height
     * @param string $alt
     * @param string $class
     * @param string $extra_html
     * @return
     */
    public static function getFlag($country, $width = null, $height = null, $alt = '', $class = '', $extra_html = '') {
        if (strlen($country) > 2) {
            $country = self::getCode($country);
        }
        if (empty($country)) {
            return '<img src="' . config_get("application.http_webroot") . '/__erra/images/spacer.gif" width=16 height=11>';
        }
        if (mb_strtolower($country) == 'eu') {
            $country = 'europeanunion';
        }
        $width = is_null($width) ? 16 : $width;
        $height = is_null($height) ? 11 : $height;
        return Erra_Core_Locale::flag(mb_strtolower($country));
    }

    /**
     * Erra_Core_Locale::flag() - Returns flagfile from country code
     *
     * @param mixed $code
     * @return
     */
    public static function flag($code) {
        return '<img src="' . config_get("application.http_webroot") . '/__erra/images/flags/' . mb_strtolower($code) . '.png">';
    }

    /**
     * Erra_Core_Locale::inUE() - Returns true if country code is in european union
     *
     * @param mixed $code
     * @return
     */
    public static function inUE($code) {
        $code = mb_strtoupper($code);
        $ue = array('AT', 'BE', 'BG', 'CY', 'CZ', 'DE', 'DK', 'EE', 'EL', 'ES',
            'FI', 'FR', 'GB', 'HU', 'IE', 'IT', 'LT', 'LU', 'LV', 'MT', 'NL',
            'PL', 'PT', 'RO', 'SE', 'SI', 'SK', 'EU');

        return in_array($code, $ue);
    }

    /**
     * L10N initialization
     *
     * Create global arrays for L10N stuff. Should be called before any work
     * with other methods.
     */
    public static function init() {
        $GLOBALS['__l10n'] = array();
        $GLOBALS['__l10n_files'] = array();
    }

    /**
     * Add a file
     *
     * Adds a l10n file in translation strings. $file should be given without
     * extension. This method will look for $file.lang.php and $file.po (in this
     * order) and retrieve the first one found.
     *
     * @param string	$file		Filename (without extension)
     */
    public static function setup($file) {
        $lang_file = $file . '.lang';
        $po_file = $file . '.po';
        $php_file = $file . '.lang.php';

        if (file_exists($php_file)) {
            require $php_file;
        } elseif (($tmp = self::getPoFile($po_file)) !== false) {
            $GLOBALS['__l10n_files'][] = $po_file;
            $GLOBALS['__l10n'] = array_merge($GLOBALS['__l10n'], $tmp);
        } elseif (($tmp = self::getLangFile($lang_file)) !== false) {
            $GLOBALS['__l10n_files'][] = $lang_file;
            $GLOBALS['__l10n'] = array_merge($GLOBALS['__l10n'], $tmp);
        } else {
            return false;
        }

        return true;
    }

    /** @ignore */
    public static function getLangFile($file) {
        if (!file_exists($file)) {
            return false;
        }

        $fp = @fopen($file, 'r');

        if ($fp === false) {
            return false;
        }

        $res = array();
        while ($l = fgets($fp)) {
            $l = trim($l);
            # Comment
            if (substr($l, 0, 1) == '#') {
                continue;
            }

            # Original text
            if (substr($l, 0, 1) == ';' && ($t = fgets($fp)) !== false && trim($t) != '') {
                $res[$l] = trim($t);
            }
        }
        fclose($fp);

        return $res;
    }

    /**
     * Load gettext file
     *
     * Returns an array of strings found in a given gettext (.po) file
     *
     * @param string	$file		Filename
     * @return array|false
     */
    public static function getPoFile($file) {
        if (!file_exists($file)) {
            return false;
        }

        $fc = implode('', file($file));

        $res = array();

        $matched = preg_match_all('/(msgid\s+("([^"]|\\\\")*?"\s*)+)\s+' .
                '(msgstr\s+("([^"]|\\\\")*?(?<!\\\)"\s*)+)/', $fc, $matches);

        if (!$matched) {
            return false;
        }

        for ($i = 0; $i < $matched; $i++) {
            $msgid = preg_replace('/\s*msgid\s*"(.*)"\s*/s', '\\1', $matches[1][$i]);
            $msgstr = preg_replace('/\s*msgstr\s*"(.*)"\s*/s', '\\1', $matches[4][$i]);

            $msgstr = self::poString($msgstr);

            if ($msgstr) {
                $res[self::poString($msgid)] = $msgstr;
            }
        }

        if (!empty($res[''])) {
            $meta = $res[''];
            unset($res['']);
        }

        return $res;
    }

    private static function poString($string, $reverse=false) {
        if ($reverse) {
            $smap = array('"', "\n", "\t", "\r");
            $rmap = array('\\"', '\\n"' . "\n" . '"', '\\t', '\\r');
            return trim((string) str_replace($smap, $rmap, $string));
        } else {
            $smap = array('/"\s+"/', '/\\\\n/', '/\\\\r/', '/\\\\t/', '/\\\"/');
            $rmap = array('', "\n", "\r", "\t", '"');
            return trim((string) preg_replace($smap, $rmap, $string));
        }
    }

    /**
     * L10N file
     *
     * Returns a file path for a file, a directory and a language.
     * If $dir/$lang/$file is not found, it will check if $dir/en/$file
     * exists and returns the result. Returns false if no file were found.
     *
     * @param string	$dir		Directory
     * @param string	$file	File
     * @param string	$lang	Language
     * @return string|false		File path or false
     */
    public static function getFilePath($dir, $file, $lang) {
        $f = $dir . '/' . $lang . '/' . $file;
        if (!file_exists($f)) {
            $f = $dir . '/en/' . $file;
        }

        return file_exists($f) ? $f : false;
    }

    /**
     * ISO Codes
     *
     * Returns an array predefined languages ISO codes as you can find on
     * {@link http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes}
     * The list as additionnal IETF codes as pt-br.
     *
     * @param boolean	$flip			Flip resulting array
     * @param boolean	$name_with_code	Prefix (code) to names
     * @return array
     */
    public static function getISOcodes($flip=false, $name_with_code=false) {
        if (empty(self::$langs)) {
            self::$langs = array(
                'aa' => 'Afaraf',
                'ab' => '�?�?Ò§Ñ�?Ñƒ�?°',
                'ae' => 'Avesta',
                'af' => 'Afrikaans',
                'ak' => 'Akan',
                'am' => 'áŠ áˆ›áˆ­áŠ›',
                'an' => 'Aragonés',
                'ar' => 'â€«Ø§Ù„Ø¹Ø±Ø¨ÙŠØ©',
                'as' => 'à¦…à¦¸à¦®à§€à¦¯à¦¼à¦¾',
                'av' => '�?°�?²�?°Ñ€ �?¼�?°Ñ†Ó€',
                'ay' => 'Aymar aru',
                'az' => 'AzÉ™rbaycan dili',
                'ba' => '�?±�?°ÑˆÒ¡�?¾Ñ€Ñ‚ Ñ‚�?µ�?»�?µ',
                'be' => '�?‘�?µ�?»�?°Ñ€ÑƒÑ�?�?º�?°Ñ�?',
                'bg' => '�?±ÑŠ�?»�?³�?°Ñ€Ñ�?�?º�?¸ �?µ�?·�?¸�?º',
                'bh' => 'à¤­à¥‹à¤œà¤ªà¥�?à¤°à¥€',
                'bi' => 'Bislama',
                'bm' => 'Bamanankan',
                'bn' => 'à¦¬à¦¾à¦‚à¦²à¦¾',
                'bo' => 'à½–à½¼à½‘à¼‹à½¡à½²à½‚',
                'br' => 'Brezhoneg',
                'bs' => 'Bosanski jezik',
                'ca' => 'CatalÃ ',
                'ce' => '�?½�?¾Ñ…Ñ‡�?¸�?¹�?½ �?¼�?¾Ñ‚Ñ‚',
                'ch' => 'Chamoru',
                'co' => 'Corsu',
                'cr' => 'á“€á�?¦á�?ƒá�?­á�?�?á�?�?á�?£',
                'cs' => 'ÄŒesky',
                'cu' => 'Ñ©�?·Ñ‹�?ºÑŠ �?¡�?»�?¾�?²Ñ£�?½ÑŒÑ�?�?ºÑŠ',
                'cv' => 'Ñ‡Ó‘�?²�?°Ñˆ Ñ‡Ó—�?»Ñ…�?¸',
                'cy' => 'Cymraeg',
                'da' => 'Dansk',
                'de' => 'Deutsch',
                'dv' => 'â€«Þ‹Þ¨ÞˆÞ¬Þ€Þ¨',
                'dz' => 'à½¢à¾«à½¼à½„à¼‹à½�?',
                'ee' => 'Æ�?Ê‹É›gbÉ›',
                'el' => 'Î•Î»Î»Î·Î½Î¹ÎºÎ¬',
                'en' => 'English',
                'eo' => 'Esperanto',
                'es' => 'espaÃ±ol',
                'et' => 'Eesti keel',
                'eu' => 'Euskara',
                'fa' => 'â€«Ù�?Ø§Ø±Ø³ÛŒ',
                'ff' => 'Fulfulde',
                'fi' => 'Suomen kieli',
                'fj' => 'Vosa Vakaviti',
                'fo' => 'FÃ¸royskt',
                'fr' => 'Français',
                'fy' => 'Frysk',
                'ga' => 'Gaeilge',
                'gd' => 'GÃ idhlig',
                'gl' => 'Galego',
                'gn' => "AvaÃ±e'áº½",
                'gu' => 'àª—à«�?àªœàª°àª¾àª¤à«€',
                'gv' => 'Ghaelg',
                'ha' => 'â€«Ù‡ÙŽÙˆÙ�?Ø³ÙŽ',
                'he' => 'â€«×¢×‘×¨×™×ª',
                'hi' => 'à¤¹à¤¿à¤¨à¥�?à¤¦à¥€',
                'ho' => 'Hiri Motu',
                'hr' => 'Hrvatski',
                'ht' => 'KreyÃ²l ayisyen',
                'hu' => 'Magyar',
                'hy' => 'Õ€Õ¡ÕµÕ¥Ö€Õ¥Õ¶',
                'hz' => 'Otjiherero',
                'ia' => 'Interlingua',
                'id' => 'Bahasa Indonesia',
                'ie' => 'Interlingue',
                'ig' => 'Igbo',
                'ii' => 'ê†‡ê‰™',
                'ik' => 'IÃ±upiaq',
                'io' => 'Ido',
                'is' => 'Ã�?slenska',
                'it' => 'Italiano',
                'iu' => 'á�?ƒá“„á’ƒá‘Žá‘�?á‘¦',
                'ja' => 'æ—¥æœ¬èªž',
                'jv' => 'Basa Jawa',
                'ka' => 'áƒ¥áƒ�?áƒ áƒ—áƒ£áƒšáƒ˜',
                'kg' => 'KiKongo',
                'ki' => 'GÄ©kÅ©yÅ©',
                'kj' => 'Kuanyama',
                'kk' => 'Òš�?°�?·�?°Ò› Ñ‚Ñ–�?»Ñ–',
                'kl' => 'Kalaallisut',
                'km' => 'áž—áž¶ážŸáž¶áž�?áŸ’áž˜áŸ‚ážš',
                'kn' => 'à²•à²¨à³�?à²¨à²¡',
                'ko' => 'í•œêµ­ì–´',
                'kr' => 'Kanuri',
                'ks' => 'à¤•à¤¶à¥�?à¤®à¥€à¤°à¥€',
                'ku' => 'KurdÃ®',
                'kv' => '�?º�?¾�?¼�?¸ �?ºÑ‹�?²',
                'kw' => 'Kernewek',
                'ky' => '�?ºÑ‹Ñ€�?³Ñ‹�?· Ñ‚�?¸�?»�?¸',
                'la' => 'Latine',
                'lb' => 'LÃ«tzebuergesch',
                'lg' => 'Luganda',
                'l1' => 'International',
                'li' => 'Limburgs',
                'ln' => 'LingÃ¡la',
                'lo' => 'àºžàº²àºªàº²àº¥àº²àº§',
                'lt' => 'LietuviÅ³ kalba',
                'lu' => 'Luba-Katanga	',
                'lv' => 'LatvieÅ¡u valoda',
                'mg' => 'Malagasy fiteny',
                'mh' => 'Kajin MÌ§ajeÄ¼',
                'mi' => 'Te reo MÄ�?ori',
                'mk' => '�?¼�?°�?º�?µ�?´�?¾�?½Ñ�?�?º�?¸ Ñ˜�?°�?·�?¸�?º',
                'ml' => 'à´®à´²à´¯à´¾à´³à´‚',
                'mn' => '�?œ�?¾�?½�?³�?¾�?»',
                'mo' => 'Limba moldoveneascÄƒ',
                'mr' => 'à¤®à¤°à¤¾à¤ à¥€',
                'ms' => 'Bahasa Melayu',
                'mt' => 'Malti',
                'my' => 'á€—á€™á€¬á€…á€¬',
                'na' => 'EkakairÅ© Naoero',
                'nb' => 'Norsk bokmÃ¥l',
                'nd' => 'isiNdebele',
                'ne' => 'à¤¨à¥‡à¤ªà¤¾à¤²à¥€',
                'ng' => 'Owambo',
                'nl' => 'Nederlands',
                'nl-be' => 'Nederlands (Belgium)',
                'nn' => 'Norsk nynorsk',
                'no' => 'Norsk',
                'nr' => 'Ndébélé',
                'nv' => 'Diné bizaad',
                'ny' => 'ChiCheÅµa',
                'oc' => 'Occitan',
                'oj' => 'á�?Šá“‚á�?‘á“ˆá�?¯á’§á�?Žá“�?',
                'om' => 'Afaan Oromoo',
                'or' => 'à¬“à¬¡à¬¼à¬¿à¬†',
                'os' => '�?˜Ñ€�?¾�?½ Ã¦�?²�?·�?°�?³',
                'pa' => 'à¨ªà©°à¨œà¨¾à¨¬à©€',
                'pi' => 'à¤ªà¤¾à¤´à¤¿',
                'pl' => 'Polski',
                'ps' => 'â€«Ù¾ÚšØªÙˆ',
                'pt' => 'Português',
                'pt-br' => 'Português (Brasil)',
                'qu' => 'Runa Simi',
                'rm' => 'Rumantsch grischun',
                'rn' => 'kiRundi',
                'ro' => 'RomÃ¢nÄƒ',
                'ru' => '�? ÑƒÑ�?Ñ�?�?º�?¸�?¹',
                'rw' => 'IKinyarwanda',
                'sa' => 'à¤¸à¤‚à¤¸à¥�?à¤•à¥ƒà¤¤à¤®à¥�?',
                'sc' => 'sardu',
                'sd' => 'à¤¸à¤¿à¤¨à¥�?à¤§à¥€',
                'se' => 'DavvisÃ¡megiella',
                'sg' => 'YÃ¢ngÃ¢ tÃ® sÃ¤ngÃ¶',
                'sh' => 'SrpskoHrvatski',
                'si' => 'à·ƒà·’à¶‚à·„à¶½',
                'sk' => 'SlovenÄ�?ina',
                'sl' => 'SlovenÅ¡Ä�?ina',
                'sm' => "Gagana fa'a Samoa",
                'sn' => 'chiShona',
                'so' => 'Soomaaliga',
                'sq' => 'Shqip',
                'sr' => 'Ñ�?Ñ€�?¿Ñ�?�?º�?¸ Ñ˜�?µ�?·�?¸�?º',
                'ss' => 'SiSwati',
                'st' => 'seSotho',
                'su' => 'Basa Sunda',
                'sv' => 'Svenska',
                'sw' => 'Kiswahili',
                'ta' => 'à®¤à®®à®¿à®´à¯�?',
                'te' => 'à°¤à±†à°²à±�?à°—à±�?',
                'tg' => 'Ñ‚�?¾Ò·�?¸�?ºÓ£',
                'th' => 'à¹„à¸—à¸¢',
                'ti' => 'á‰µáŒ�?áˆ­áŠ›',
                'tk' => 'TÃ¼rkmen',
                'tl' => 'Tagalog',
                'tn' => 'seTswana',
                'to' => 'faka Tonga',
                'tr' => 'TÃ¼rkÃ§e',
                'ts' => 'xiTsonga',
                'tt' => 'Ñ‚�?°Ñ‚�?°Ñ€Ñ‡�?°',
                'tw' => 'Twi',
                'ty' => 'Reo MÄ�?`ohi',
                'ug' => 'UyÆ£urqÉ™',
                'uk' => '�?£�?ºÑ€�?°Ñ—�?½Ñ�?ÑŒ�?º�?°',
                'ur' => 'â€«Ø§Ø±Ø¯Ùˆ',
                'uz' => "O'zbek",
                've' => 'tshiVená¸“a',
                'vi' => 'Tiáº¿ng Viá»‡t',
                'vo' => 'VolapÃ¼k',
                'wa' => 'Walon',
                'wo' => 'Wollof',
                'xh' => 'isiXhosa',
                'yi' => 'â€«×™×™Ö´×“×™×©',
                'yo' => 'YorùbÃ¡',
                'za' => 'SaÉ¯ cueÅ‹Æ…',
                'zh' => 'ä¸­æ–‡',
                'zh-hk' => 'ä¸­æ–‡ (é¦™æ¸¯)',
                'zh-tw' => 'ä¸­æ–‡ (è‡ºç�?£)',
                'zu' => 'isiZulu'
            );
        }

        $langs = self::$langs;
        if ($name_with_code) {
            foreach ($langs as $k => &$v) {
                $v = '(' . $k . ') ' . $v;
            }
        }

        if ($flip) {
            return array_flip($langs);
        }

        return $langs;
    }

    /**
     * Text direction
     *
     * Returns text direction for a given language.
     * If text direction was forced with {@link $text_direction}, returns this
     * value.
     *
     * @param string	$lang	Language code
     * @return string
     */
    public static function getTextDirection($lang) {
        if (self::$text_direction) {
            return self::$text_direction;
        }

        if (preg_match('/^(ar|dv|fa|ha|he|ps|ur|yi)$/i', $lang)) {
            return 'rtl';
        }
        return 'ltr';
    }

    public static function getRealIp() {
        if (!empty($_SERVER['HTTP_CLIENT_IP'])) {   //check ip from share internet
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {   //to check ip is pass from proxy
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        } else {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        return $ip;
    }

    public static function countryCityFromIP($ipAddr) {
        ip2long($ipAddr) == -1 || ip2long($ipAddr) === false ? trigger_error("Invalid IP", E_USER_ERROR) : "";
        $ipDetail = array();
        if (isset($ipDetail['city']) && isset($ipDetail['country']) && isset($ipDetail['country_code'])) {
            $xml = file_get_contents_curl("http://api.hostip.info/?ip=" . $ipAddr);
            preg_match("@<Hostip>(\s)*<gml:name>(.*?)</gml:name>@si", $xml, $match);
            $ipDetail['city'] = $match[2];
            preg_match("@<countryName>(.*?)</countryName>@si", $xml, $matches);
            $ipDetail['country'] = $matches[1];
            preg_match("@<countryAbbrev>(.*?)</countryAbbrev>@si", $xml, $cc_match);
            $ipDetail['country_code'] = $cc_match[1];
        }
        return $ipDetail;
    }

    public static function getGeolocalizeCountryCode() {
        // Try with IP first
        $location = self::countryCityFromIP(self::getRealIp());
        if (!isset($location['country_code']) || $location['country_code'] == 'XX') {
            // Try with browser langage
            $langcode = (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : '';
            $langcode = (!empty($langcode)) ? explode(";", $langcode) : $langcode;
            $langcode = (!empty($langcode['0'])) ? explode(",", $langcode['0']) : $langcode;
            $langcode = (!empty($langcode['0'])) ? explode("-", $langcode['0']) : $langcode;
            return $langcode['0'];
        }
        return $location['country_code'];
    }

}

?>